#include <cv.h>
#include <highgui.h>
#include <math.h>

#define N 300

using namespace cv;

int entropy(Mat img)
{
    Mat hsv;
    //hsv=img;
    //cvtColor(img, hsv, CV_BGR2HSV);
    cvtColor(img, hsv, CV_BGR2Lab);
    //cvtColor(img, hsv, CV_RGB2Lab);
    //cvtColor(img, hsv, CV_RGB2HSV);

    // Quantize the hue to 30 levels
    // and the saturation to 32 levels
    int hbins = 10, sbins = 32, zbins = 32;
    int histSize[] = {hbins};
    // hue varies from 0 to 179, see cvtColor
    float hranges[] = { 0, 100 };
    // saturation varies from 0 (black-gray-white) to
    // 255 (pure spectrum color)
    float sranges[] = { -127, 127 };
    float zranges[] = { -127, 127 };

    const float* ranges[] = { hranges };
    MatND hist;
    // we compute the histogram from the 0-th and 1-st channels
    int channels[] = {2};

    calcHist( &hsv, 1, channels, Mat(), // do not use mask
             hist, 1, histSize, ranges,
             true, // the histogram is uniform
             false );
    //cvNormalizeHist(hist, factor)
    //ENTROPY
    double H=0;	
    for( int h = 0; h < hbins; h++ ) 
           {
            float binVal = hist.at<float>(h);
	    if (binVal != 0) {
	   	    H+=binVal*log(float(binVal));	
	    }
        }
    //printf("ENTROPY:\%f\n",H);
    return H;
}


int main( int argc, char** argv )
{
    Mat src;	
    double H[N];
    double min=1000000000000,max=0;
    for(int i=0;i<N;i++)
    {
        char filename[150];
        sprintf(filename, "../img/luna-%d.png", (i+1));
        if(!(src=imread(filename, 1)).data ) return -1;
        H[i]=entropy(src);
        //printf("%d :  %f\n",i+1,H[i]);
        if (min >=H[i]) min=H[i]; 
        if (max <=H[i]) max=H[i];
    }
    //NORMALIZE to 100
    double h,minmax=max-min;
    for(int i=0;i<N;i++)
    {
       h=100-(H[i]-min)*100/minmax;
       printf("%d :  %f\n",i+1,h);
    }
	

/*    if( argc != 2 || !(src=imread(argv[1], 1)).data )
        return -1; */

}



